<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div id="app">
        <input v-model="student" type="text" @keyup.enter="addStudent" />
        <p>{{studentCount}}</p>
        <ul>
            <li v-for="(item,index) in students" :key="index">
                {{index+1}}.{{ item }}
            </li>
        </ul>
    </div>
    <script src="../assets/vue2.js"></script>
    <script>
        const app = new Vue({
            el: '#app',
            data() {
                return {
                    student: '', // v-model初始化
                    studentCount: 0, // 初始化
                    students: ['张三', '李四', '露西', '王五']
                }
            },
            methods: {
                addStudent() {
                    this.students.push(this.student);
                    this.student = '';
                }
            },
            watch: {
                // 不加特殊声明，变化之后才会执行
                students(newValue, oldValue) {
                    this.studentCount = newValue.length;
                }
            },
        });
    </script>

</body>

</html>